package day15.work;

import com.rabbitmq.client.*;
import util.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @ClassName Rec1
 * @Description Rec1
 * @Author 孙峰
 * @Date 2020/4/1 9:23
 * @Version 1.0
 */
public class Rec2 {
    private final static String QUEUE_NAME = "test_work_queue";
    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME,false,false, false, null);
        //告诉RabbitMQ一次不要向工作人员发送多于一条消息
        channel.basicQos(1);
        //  设置消费者
        DefaultConsumer consumer=    new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body);
                System.out.println("[消费者2]接受消息"+msg+"!");

                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };
        //监听队列
        channel.basicConsume(QUEUE_NAME,false,consumer);

    }
}
